package com.hc.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.hc.bean.OrderDetail;
import com.hc.bean.OrderInfo;
import com.hc.bean.Pager;
import com.hc.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/orderinfo")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(OrderInfo orderInfo, Integer page, Integer rows) {
//        System.out.println(orderInfo);
        if ("请选择".equals(orderInfo.getStatus())) {
            orderInfo.setStatus(null);
        }
        List<OrderInfo> orderInfoList = null;
        Pager pager = new Pager();
        pager.setPage(page);
        pager.setPageRows(rows);
        Map<String, Object> resultMap = new HashMap<>();
        int OrderCount = orderInfoService.getOrderInfoCount(orderInfo);
//            System.out.println("OrderCount======="+OrderCount);
        orderInfoList = orderInfoService.getOrderInfoList(orderInfo, pager);
//        System.out.println("orderInfoList**********"+orderInfoList);
        resultMap.put("total", OrderCount);
        resultMap.put("rows", orderInfoList);
        return resultMap;
    }

    @RequestMapping("/getOrderInfo")
//    @ResponseBody //返回字符串且进行页面跳转,不需要加ResponseBody注解
    public String getOrderInfo(Integer oid, Model model) {
//        System.out.println(oid);
        OrderInfo orderInfo = orderInfoService.getOrderInfoById(oid);
        model.addAttribute("orderInfo", orderInfo);
        System.out.println(orderInfo);
        return "orderdetail";
    }

    @RequestMapping("/getOrderDetails")
    @ResponseBody //返回字符串且进行页面跳转,不需要加ResponseBody注解
    public List<OrderDetail> getOrderDetails(Integer oid) {
        return orderInfoService.getOrderDetailByOid(oid);
    }

    @RequestMapping("/commitOrder")
    @ResponseBody //返回字符串且进行页面跳转,不需要加ResponseBody注解
    public String commitOrder(String inserted,String orderinfo) {
        List<OrderDetail> orderDetailList= JSON.parseArray(inserted,OrderDetail.class);
        List<OrderInfo> orderInfoList=JSON.parseArray(orderinfo,OrderInfo.class);
//        System.out.println(orderDetailList);
//        System.out.println(orderInfoList);
       /* ObjectMapper mapper = new ObjectMapper();
//        设置输入时 忽略 JSon字符串中 存在  Java对象没有的 属性
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
//        Json 转换 为 Java对象
        OrderInfo oi = mapper.readValue(orderinfo,OrderInfo[].class)[0];
//        Json 转换为  Java集合对象
        List<OrderDetail> details = mapper.readValue(inserted, new TypeReference<ArrayList<OrderDetail>>(){});*/
        if(orderInfoService.addOrder(orderInfoList.get(0),orderDetailList))
            return "success";
        return null;
    }


    @RequestMapping("/deleteOrder")
    @ResponseBody //返回字符串且进行页面跳转,不需要加ResponseBody注解
    public Map<String,Object> deleteOrder(String oids) {
        oids=oids.substring(0,oids.length()-1);
        Map<String,Object> resultMap=new HashMap<>();
        if(orderInfoService.deleteOrderInfoByOids(oids)){
            resultMap.put("success","true");
            resultMap.put("message","订单删除成功!");
        }else {
            resultMap.put("success","false");
            resultMap.put("message","订单删除失败!");
        }
        return resultMap;
    }
}
